<!DOCTYPE html>
<html>

<head>
  <title>Test the Geolocation API during prerendering</title>
</head>

<body>
  <script>

    const eventsSeen = [];

    let resolveFunc;
    let rejectFunc;

    document.addEventListener('prerenderingchange', () => {
      eventsSeen.push(
        `prerenderingchange (prerendering: ${document.prerendering})`);
    });

    const result = new Promise((resolve, reject) => {
      resolveFunc = resolve;
      rejectFunc = reject;
    });

    async function accessGeolocation() {
      try {
        // The page should initially be in the prerendering state.
        eventsSeen.push(
          `accessGeolocation (prerendering: ${document.prerendering})`);

        const geolocationStatus = await navigator.permissions.query({
          name: 'geolocation'
        });

        if (PermissionStatus.state === 'granted') {
          rejectFunc('unexpected PermissionStatus.state '
            + PermissionStatus.state);
        }

        // This will trigger a permission prompt.
        navigator.geolocation.getCurrentPosition(_ => { }, _ => { });


        // `navigator.geolocation.getCurrentPosition()` is expected to be
        // deferred until the prerendered page is activated. So
        // `document.prerendering` should be false here.
        eventsSeen.push(
          `getCurrentPosition (prerendering: ${document.prerendering})`);

        resolveFunc(true);
      } catch (err) {
        rejectFunc(err);
      }
    }

    function runLoop() {
      return new Promise(resolve => {
        setTimeout(resolve, 0);
      });
    }

  </script>
</body>

</html>
